package project7.ApplicationLogic;



import java.util.ArrayList;
import project7.Storage.*;

/**
 *
 * This is the main controller for the business logic of Project7
 * 
 * @author project7
 */
public class Project7Controller {
    private StorageController storage;
    /**
     * This creates a new controller and associates the controller with the current running StorageController.
     */
    public Project7Controller(){

        storage = StorageController.getInstance();

    }

    /**
     * This checks a particular table to see if it is open.
     * @param tableNumber int of the table to check
     * @return true if table is open, false otherwise
     */
    public boolean validateOpenTable(int tableNumber)
    {
        return storage.getTable(tableNumber,true).isOpen();
    }
    /**
     * TO BE IMPLEMENTED
     * This authenticates a server into the database system.
     * Multiple logons are allowed, but any additional logon is not timestamped.
     * This returns true on a successful logon, false otherwise.
     *
     * @param serverCode ID/Code of server to authenticate
     * @return true for successful logon, false for failure
     */
    public boolean authenticateServer(int serverPass) {
        boolean rvalue = false;
        rvalue = storage.checkCode(serverPass);
        return rvalue;
    }
    /**
     * This finds a table according to a table number.
     * @param table number fo table to open
     * @return reference to Table object, null if table is not found
     */
    public Table getTable(int table, boolean is_open) {
        return storage.getTable(table,is_open);
    }
    /**
     * This searches the database for a MenuItem specified by itemName.
     * @param itemName to search for in the database
     * @return reference to the MenuItem, null if it is not found
     */
    public MenuItem getMenuItem(String itemName) {
        return storage.getMenuItem(itemName);
    }
    /**
     * This searches the database for a Server by the id number.
     * @param serverCode ID/Code of server to search for
     * @return reference to the Server, or null if the serverCode is not found
     */
    public Server getServer(int serverCode) {
        return storage.getServer(serverCode);
    }
    /**
     * This gets the check object in used by a server at a table.
     * @param tableNumber table that has to pay for the check
     * @param serverCode server that is taking care of the check
     * @return reference to the Check, or null if it is not found
     */
    public Check getCheck(int tableNumber, int serverCode) {
        Check rvalue = null;
        Table openTable = storage.getTable(tableNumber,true);
        if ((openTable.getServer().getCode() == serverCode) && openTable.isOpen())
            rvalue = openTable.getCheck();

        return rvalue;
    }
    /**
     * This returns all the menu items.
     * @return ArrayList of all menu items
     */
    public ArrayList<MenuItem> getMenuItems() {
        return storage.getMenuItems();
    }
    /**
     * This returns all the checks by a single server.
     * @param serverCode ID/Code of an individual server
     * @return ArrayList of the Checks opened by the server
     */
    public ArrayList<Check> getChecksByServer(int serverCode) {
        ArrayList<Check> rvalue = new ArrayList<Check>();

        Server serv = storage.getServer(serverCode);
        ArrayList<Table> tables = serv.getOpenTables();

        for (Table tab : tables)
            if (tab.isOpen()) rvalue.add(tab.getCheck());

        return rvalue;
    }
    /**
     * This gets a table by tableNumber and verified by serverCode.
     * @param tableNumber number of the Table to check
     * @param serverCode Code/ID of the Server who has the table
     * @return Table reference if all is correct, null otherwise
     */
    public Table getTable(int tableNumber, int serverCode, boolean is_open) {
        Table rvalue = storage.getTable(tableNumber,is_open);

        if (rvalue.getServer().getCode() != serverCode)
            return null;
        return rvalue;
    }
    /**
     * This returns an ArrayList of all tables opened by a server.
     * @param serverCode Server whose tables we want to find
     * @return ArraList of Tables opened by Server
     */
    public ArrayList<Table> getTablesByServer(int serverCode, boolean is_open) {
        return storage.getServer(serverCode).getOpenTables();
    }
    /**
     * TO BE IMPLEMENTED
     * This will save MenuItem item into the database.
     * @param item MenuItem of what is to be added
     */
    public void saveMenuItem(MenuItem item) {

    }
    /**
     * TO BE IMPLEMENTED
     * This will save a new Server server into the database.
     * @param server Server we want to add
     */
    public void saveServer(Server server) {

    }
    /**
     * TO BE IMPLEMENTED
     * @param check
     */
    public void saveCheck(Check check) {

    }
    /**
     * TO BE IMPLEMENTED
     * @param table
     */
    public void saveTable(Table table) {}
    /**
     * THIS IS DEPRICATED
     */
    public void displayTablesScreen() {}
    /**
     * THIS IS DEPRICATED
     */
    public void displayLoginScreen() {}
    /**
     * THIS IS DEPRICATED
     */
    public void displayCheckScreen() {}
    /**
     * THIS IS DEPRICATED
     * This will save a current transaction to the database.
     * A transaction is defined by a Server who is waiting on an open Table and keeping an order on a Check
     * These three objects are required to keep the relation
     * @param server Server that is waiting on the Table
     * @param table Table where the check is
     * @param check Check containing the order
     * @return true if the order was succesfully processed, false otherwise
     */
    public boolean saveTransaction(Server server, Table table, Check check) {
        boolean rvalue = false;
        return rvalue;
    }

}
